package com.songyz.image.compress.utils;

import java.io.File;
import java.io.IOException;

import com.songyz.image.compress.dto.ImageCompressParam;
import com.songyz.image.compress.enums.CompressModel;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

public class ThumbnailsUtils {

    public static void compress(ImageCompressParam param, File source, File target) throws IOException {

        Builder<File> builder = Thumbnails.of(source);

        CompressModel model = CompressModel.get(param.getModel());

        switch (model) {
            case QUALITY:
                builder.scale(1).outputQuality(param.getQuality());
                break;
            case SCALE:
                builder.scale(param.getScale());
                break;
            case CUSTOM:
                if (param.isForce())
                    builder.forceSize(param.getWitth(), param.getHeight());
                else
                    builder.size(param.getWitth(), param.getHeight());

                builder.scale(param.getScale()).outputQuality(param.getQuality());
                break;
            case DEFAULT:
            default:
                builder.scale(1);
                break;
        }

        builder.toFile(target);
    }

}
